import { IStorage, MemoryStorage } from './MemoryStorage'

export class Storage {
  static storages: Map<string, IStroage> = new Map();
  static readonly DEFAULT_KEY: string = '__d';

  static default(): IStorage {
    if (!Storage.storages.has(Storage.DEFAULT_KEY)) {
      Storage.storages.set(Storage.DEFAULT_KEY, new MemoryStorage(Storage.DEFAULT_KEY));
    }
    return Storage.storages.get(Storage.DEFAULT_KEY);
  }

  static fromNs(namespace: string): IStorage {
    if (!Storage.storages.has(namespace)) {
      Storage.storages.set(namespace, new MemoryStorage(namespace));
    }
    return Storage.storages.get(namespace);
  }

  static clear(): boolean {
    Storage.storages.forEach((value, key) => {
      value.clear();
    });
    return true;
  }
}

